# encoding=utf8

import http.client
import json

conn = http.client.HTTPConnection("www.pm25.in")

conn.request("GET", "/api/querys/pm2_5.json?token=5j1znBVAsnSf5xQyNQyq&city=suzhou")
res = conn.getresponse()

if res.status != http.client.OK:
    print("error response code {status} ,reason {reason}".format(status=res.status, reason=res.reason))

data = res.read()
raw = data.decode("utf-8")
raw = json.dumps([{'pm2_5_24h': 36, 'quality': '优', 'position_name': '上方山', 'primary_pollutant': None, 'pm2_5': 29, 'station_code': '1160A',
                   'time_point': '2015-09-05T21:00:00Z', 'aqi': 42, 'area': '苏州'},
                  {'pm2_5_24h': 42, 'quality': '优', 'position_name': '南门', 'primary_pollutant': None, 'pm2_5': 30, 'station_code': '1161A',
                   'time_point': '2015-09-05T21:00:00Z', 'aqi': 43, 'area': '苏州'},
                  {'pm2_5_24h': 34, 'quality': '优', 'position_name': '彩香', 'primary_pollutant': None, 'pm2_5': 31, 'station_code': '1162A',
                   'time_point': '2015-09-05T21:00:00Z', 'aqi': 45, 'area': '苏州'},
                  {'pm2_5_24h': 41, 'quality': '优', 'position_name': '轧钢厂', 'primary_pollutant': None, 'pm2_5': 28, 'station_code': '1163A',
                   'time_point': '2015-09-05T21:00:00Z', 'aqi': 40, 'area': '苏州'},
                  {'pm2_5_24h': 32, 'quality': '优', 'position_name': '吴中区', 'primary_pollutant': None, 'pm2_5': 19, 'station_code': '1164A',
                   'time_point': '2015-09-05T21:00:00Z', 'aqi': 42, 'area': '苏州'},
                  {'pm2_5_24h': 41, 'quality': '优', 'position_name': '苏州新区', 'primary_pollutant': None, 'pm2_5': 35, 'station_code': '1165A',
                   'time_point': '2015-09-05T21:00:00Z', 'aqi': 50, 'area': '苏州'},
                  {'pm2_5_24h': 41, 'quality': '优', 'position_name': '苏州工业园区', 'primary_pollutant': None, 'pm2_5': 31, 'station_code': '1166A',
                   'time_point': '2015-09-05T21:00:00Z', 'aqi': 45, 'area': '苏州'},
                  {'pm2_5_24h': 36, 'quality': '优', 'position_name': '相城区', 'primary_pollutant': None, 'pm2_5': 33, 'station_code': '1167A',
                   'time_point': '2015-09-05T21:00:00Z', 'aqi': 48, 'area': '苏州'},
                  {'pm2_5_24h': 37, 'quality': '良', 'position_name': 'xxx', 'primary_pollutant': '臭氧8小时', 'pm2_5': 29, 'station_code': None,
                   'time_point': '2015-09-05T21:00:00Z', 'aqi': 71, 'area': '苏州'}])

jsonObj = json.loads(raw, encoding="utf-8")

if isinstance(jsonObj, dict):
    if "error" in jsonObj:
        print(jsonObj["error"])
    else:
        print("unknown result", jsonObj)
else:
    for row in jsonObj:
        print("地点:{0},\npm2.5 : {1},\n质量:{2}\n".format(row["position_name"], row["pm2_5"], row["quality"]))
        # print("地点:", row["position_name"], "pm2.5", row["pm2_5"])
